package com.lee.blepwm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lee.blepwm.callback.OnAdapterItemClickListener;
import com.lee.blepwm.model.BleModel;
import com.lee.blepwm.R;

import java.util.List;

public class BleAdapter extends RecyclerView.Adapter<BleAdapter.ViewHolder> {
    private Context mContext;
    private List<BleModel> mData;
    private OnAdapterItemClickListener onAdapterItemClickListener;

    public BleAdapter(Context mContext, List<BleModel> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.ble_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tvName.setText(mData.get(position).getDeviceName());

        holder.itemView.setOnClickListener(view -> {
            if (onAdapterItemClickListener != null)
                onAdapterItemClickListener.onItemClick(position);
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }

    public void setOnAdapterItemClickListener(OnAdapterItemClickListener onAdapterItemClickListener) {
        this.onAdapterItemClickListener = onAdapterItemClickListener;
    }
}
